package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.common.util.JsonUtil;
import com.coolbi.onsupai.core.biz.TransportModeBiz;
import com.coolbi.onsupai.core.entity.TransportMode;
import com.coolbi.security.web.action.SessionAction;

public class TransportModeAction extends SessionAction {

	private static final long serialVersionUID = 1L;

	private TransportMode transportMode;
	private List<TransportMode> transportModeList;
	@Autowired
	private TransportModeBiz transportModeBiz;

	/**
	 * 分页查询运输方式
	 * @return
	 */
	public String listByPage() {
		try {
			pager.setRecordCount(transportModeBiz.count());
			transportModeList = transportModeBiz.listByPage(
					(pager.getCurrPageNo() - 1) * pager.getPageSize(),
					pager.getPageSize());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 添加运输方式
	 * @return
	 */
	public String save() {
		try {
			transportModeBiz.save(transportMode);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	/**
	 * 更新运输方式
	 * @return
	 */
	public String update() {
		try {
			transportModeBiz.update(transportMode);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	/**
	 * 删除运输方式
	 * @return
	 */
	public String delete() {
		try {
			transportModeBiz.delete(transportMode.getId());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 根据ID查看运输方式
	 * @return
	 */
	public String findById() {
		try {
			transportMode = transportModeBiz.get(transportMode.getId());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * 获取所有运输方式,返回Json数据
	 * @return
	 */
	public String findAllTransportMode() {
		transportModeList = transportModeBiz.list();
		String[] fields = {"modeName", "id"};
		String json = JsonUtil.toJsonByArray(transportModeList, fields);
		return ajax(json);
	}
	
	public TransportMode getTransportMode() {
		return transportMode;
	}

	public void setTransportMode(TransportMode transportMode) {
		this.transportMode = transportMode;
	}

	public List<TransportMode> getTransportModeList() {
		return transportModeList;
	}

	public void setTransportModeList(List<TransportMode> transportModeList) {
		this.transportModeList = transportModeList;
	}

	public void setTransportModeBiz(TransportModeBiz transportModeBiz) {
		this.transportModeBiz = transportModeBiz;
	}
}
